package com.speedata

import android.app.Instrumentation
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.KeyEvent
import android.view.View
import android.widget.Button
import android.widget.Toast
import com.speedata.uhftest.R


/**
 * @author xuyan
 */
class MengActivity : AppCompatActivity(), View.OnClickListener {

    private var mButton1: Button? = null
    private var mButton2: Button? = null
    private var mButton3: Button? = null

    private var mbt1: Boolean = false
    private var mbt2: Boolean = false
    private var mbt3: Boolean = false


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_meng)
        initView()
//        startActivity(Intent(this@MengActivity, FlexboxTextActivity::class.java))
//        finish()
    }

    private fun initView() {
        mButton1 = findViewById(R.id.bt1)
        mButton2 = findViewById(R.id.bt2)
        mButton3 = findViewById(R.id.bt3)

        mButton1!!.setOnClickListener(this)
        mButton2!!.setOnClickListener(this)
        mButton3!!.setOnClickListener(this)

        mbt1 = false
        mbt2 = false
        mbt3 = false

    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.bt1 -> {
                if (mbt1) {
                    mButton1!!.setBackgroundColor(resources.getColor(R.color.colorPrimary))
                    mbt1 = false
                } else {
                    mButton1!!.setBackgroundColor(resources.getColor(R.color.colorAccent))
                    mbt1 = true
                }

                down2(KeyEvent.KEYCODE_BUTTON_1)
            }
            R.id.bt2 -> {
                if (mbt2) {
                    mButton2!!.setBackgroundColor(resources.getColor(R.color.colorPrimary))
                    mbt2 = false
                } else {
                    mButton2!!.setBackgroundColor(resources.getColor(R.color.colorAccent))
                    mbt2 = true
                }
                down2(KeyEvent.KEYCODE_BUTTON_2)
            }
            R.id.bt3 -> {
                if (mbt3) {
                    mButton3!!.setBackgroundColor(resources.getColor(R.color.colorPrimary))
                    mbt3 = false
                } else {
                    mButton3!!.setBackgroundColor(resources.getColor(R.color.colorAccent))
                    mbt3 = true
                }
                down2(KeyEvent.KEYCODE_BUTTON_3)
            }
            else -> {
            }
        }
    }

    private fun down2(keycodeButton1: Int) {
        val t = Thread(Runnable { Instrumentation().sendKeyDownUpSync(keycodeButton1) })
        t.start()
    }


    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        Toast.makeText(this, keyCode.toString(), Toast.LENGTH_SHORT).show()
        Log.d("onkeydown", keyCode.toString())
        return super.onKeyDown(keyCode, event)
    }


}
